cmake_minimum_required(VERSION 2.6)
project(mytget)

set(PROJECT_MAJOR_VERSION 1)
set(PROJECT_MINOR_VERSION 1)
set(PROJECT_PATCH_VERSION 2)

set(PROJECT_VERSION ${PROJECT_MAJOR_VERSION}.${PROJECT_MINOR_VERSION}.${PROJECT_PATCH_VERSION})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wdeprecated-declarations -O0 -ggdb -D_FILE_OFFSET_BITS=64")

INCLUDE (CheckFunctionExists)
INCLUDE (CheckIncludeFiles)
INCLUDE (CheckTypeSize)

CHECK_FUNCTION_EXISTS(alloca  C_ALLOCA)
CHECK_FUNCTION_EXISTS(memcmp  HAVE_MEMCMP)
CHECK_FUNCTION_EXISTS(strndup HAVE_STRNDUP)
CHECK_FUNCTION_EXISTS(strnlen HAVE_STRNLEN)
CHECK_FUNCTION_EXISTS(bzero HAVE_BZERO)
CHECK_FUNCTION_EXISTS(gettimeofday HAVE_GETTIMEOFDAY)
CHECK_FUNCTION_EXISTS(memmove HAVE_MEMMOVE)
CHECK_FUNCTION_EXISTS(mkdir HAVE_MKDIR)
CHECK_FUNCTION_EXISTS(select HAVE_SELECT)

CHECK_INCLUDE_FILES(alloca.h HAVE_ALLOCA_H)
CHECK_INCLUDE_FILES(strings.h HAVE_STRINGS_H)
CHECK_INCLUDE_FILES(string.h HAVE_STRING_H)
CHECK_INCLUDE_FILES(stdlib.h HAVE_STDLIB_H)
CHECK_INCLUDE_FILES(sys/time.h HAVE_SYS_TIME_H)
CHECK_INCLUDE_FILES(sys/times.h HAVE_SYS_TIMES_H)
CHECK_INCLUDE_FILES(unistd.h HAVE_UNISTD_H)
CHECK_INCLUDE_FILES(memory.h HAVE_MEMORY_H)
CHECK_INCLUDE_FILES(stdarg.h HAVE_STDARG_PROTOTYPES)
CHECK_INCLUDE_FILES(sys/un.h HAVE_SYS_UN)
CHECK_INCLUDE_FILES(arpa/inet.h HAVE_ARPA_INET)
CHECK_INCLUDE_FILES(fcntl.h HAS_FCNTL)
CHECK_INCLUDE_FILES(netdb.h HAS_NETDB)
CHECK_INCLUDE_FILES(sys/ioctl.h HAS_SYS_IOCTL)
CHECK_INCLUDE_FILES(sys/socket.h HAS_SYS_SOCKET)
CHECK_INCLUDE_FILES(sys/time.h HAS_SYS_TIME)
CHECK_INCLUDE_FILES(termios.h HAS_TERMIOS)
CHECK_INCLUDE_FILES(unistd.h HAS_UNISTD)

CHECK_TYPE_SIZE("int" SIZEOF_INT)
CHECK_TYPE_SIZE("long" SIZEOF_LONG)
CHECK_TYPE_SIZE("short" SIZEOF_SHORT)
CHECK_TYPE_SIZE("size_t" SIZEOF_SIZE_T)

if (NOT HAVE_STRNDUP)
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNO_STRNDUP")
endif(NOT HAVE_STRNDUP)

if (NOT HAVE_STRNLEN)
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNO_STRNLEN")
endif(NOT HAVE_STRNLEN)

if (NOT HAVE_SYS_UN)
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNO_SYS_UN")
endif(NOT HAVE_SYS_UN)

OPTION(HAVE_SSL      "Enable ssl support"      ON) 
SET(CMAKE_INCLUDE_CURRENT_DIR ON)
SET(HAVE_SSL ON) 

message("Build Type: ${CMAKE_BUILD_TYPE}")
message("Std CFLAGS: ${CMAKE_C_FLAGS}")
message("Dbg CFLAGS: ${CMAKE_C_FLAGS_DEBUG}")
message("Rel CFLAGS: ${CMAKE_C_FLAGS_RELEASE}")

FIND_PACKAGE (Threads REQUIRED)
find_package(OpenSSL)

include_directories(
	${CMAKE_CURRENT_SOURCE_DIR}
)

set(MYTGET_EXTERNAL_LIBS
	${CMAKE_THREAD_LIBS_INIT}
)

if (NOT OPENSSL_FOUND)
	message("Diabling SSL")
	set (HAVE_SSL OFF)
	set (OPENSSL_INCLUDE_DIR "")
	set (OPENSSL_LIBRARIES "")
else()
	add_definitions(-DHAVE_SSL)
	include_directories( ${OPENSSL_INCLUDE_DIR})
	list(APPEND MYTGET_EXTERNAL_LIBS ${OPENSSL_LIBRARIES})
endif()


if (NOT ${CMAKE_BUILD_TYPE} STREQUAL "Debug")
  set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNDEBUG")
endif (NOT ${CMAKE_BUILD_TYPE} STREQUAL "Debug")

ADD_SUBDIRECTORY(src)
